package com.xiaobo.client;

import java.util.ArrayList;
import java.util.List;



import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;

import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.Toast;

public class SysTab_Cx extends Activity {

	String cxdy;
	List<CheckBox> list = new ArrayList<CheckBox>();
	CheckBox cx_1;
	CheckBox cx_2;
	CheckBox cx_3;
	CheckBox cx_4;
	CheckBox cx_5;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Window win = getWindow();
		win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.systabcx);

		SharedPreferences shareMSG = this.getSharedPreferences("xt_cxdy",
				Context.MODE_WORLD_WRITEABLE | Context.MODE_WORLD_WRITEABLE);
		String cx = shareMSG.getString("cxdy", null);
		if (cx == null) {
			cx = "TTTFT";
			SharedPreferences.Editor editor = shareMSG.edit();
			editor.putString("cxdy", cx);
			editor.commit();
		}

		//System.out.println("fasffsa=====" + cx);
		cxdy = cx;

		cx_1 = (CheckBox) findViewById(R.id.cx_1);
		cx_2 = (CheckBox) findViewById(R.id.cx_2);
		cx_3 = (CheckBox) findViewById(R.id.cx_3);
		cx_4 = (CheckBox) findViewById(R.id.cx_4);
		cx_5 = (CheckBox) findViewById(R.id.cx_5);
		list.add(cx_1);
		list.add(cx_2);
		list.add(cx_3);
		list.add(cx_4);
		list.add(cx_5);
		InitDate();
	}

	private void InitDate() {
		// 初始化按钮数据

		Button bt1 = (Button) findViewById(R.id.cx_save);
		bt1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				SaveCx();
			}
		});
		Button bt2 = (Button) findViewById(R.id.cx_exit);
		bt2.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Exit();
			}
		});

		for (int i = 0; i < 5; i++) {

			if (cxdy.charAt(i) == 'T') {
				list.get(i).setChecked(true);
			} else {
				list.get(i).setChecked(false);
			}
		}
	}

	private void SaveCx() {
		// 保存更改数据


		for (int i = 0; i < 5; i++) {

			if (list.get(i).isChecked()) {
				cxdy=replaceCharAt(cxdy, i, 'T');
			} else {
				cxdy=replaceCharAt(cxdy, i, 'F');
			}
		}
		SharedPreferences shareMSG = this.getSharedPreferences("xt_cxdy",
				Context.MODE_WORLD_WRITEABLE | Context.MODE_WORLD_WRITEABLE);
		SharedPreferences.Editor editor = shareMSG.edit();
		editor.putString("cxdy", cxdy);
		editor.commit();
		Toast toast = Toast.makeText(getApplicationContext(), "数据保存成功！！",
				Toast.LENGTH_LONG);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();
	}

	private void Exit() {
		this.finish();
	}

	public String replaceCharAt(String s, int pos, char c) {
		return s.substring(0, pos) + c + s.substring(pos + 1);
	}

}
